package com.rainng.coursesystem.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rainng.coursesystem.dao.RegisterDao;
import com.rainng.coursesystem.dao.mapper.RegisterMapper;
import com.rainng.coursesystem.model.bo.RegisterStatusBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl extends ServiceImpl<RegisterMapper, RegisterStatusBO> implements RegisterService{

    @Autowired
    RegisterMapper registerMapper;

    @Override
    public String register(RegisterDao ro) {
        String ans = istrue(ro);
        if(ans.equals("OK")){
            RegisterStatusBO register = new RegisterStatusBO();
            register.setNumber(ro.getNumber());register.setPassword(ro.getPassword());
            registerMapper.insert(register);
            return "注册成功";
        }
        return ans;
    }
    public String istrue(RegisterDao ro){
        if(ro.getNumber()==null||ro.getNumber()==""){
            return "用户名不能为空";
        }
        if(ro.getUserType()==1 && registerMapper.m_selectByNumber(ro.getNumber())!=null){
            return "该账号已经存在";
        }
        if(ro.getUserType()==2 && registerMapper.l_selectByNumber(ro.getNumber())!=null){
            return "该账号已经存在";
        }
        if(ro.getPassword()==null||ro.getPassword()==""){
            return "密码不能为空";
        }
        if(ro.getPassword().length()!=6){
            return "密码长度只能在4~16位";
        }
        return "OK";
    }
}
